{{extend 'layout.html'}}

<!-- load dojo and provide config via data attribute -->
<script src="{{=URL('static','dojo/dojo/dojo.js')}}" data-dojo-config="async: true, parseOnLoad: true"></script>


<style>

.form_grilla_pendientes {
	font-family: "Lucida Grande", sans-serif;
	font-size: 11px;
	position: relative;
}

/* styles for autoheight */
#grid {
	
	height: auto;
}
#grid .dgrid-scroller {
	position: relative;
	overflow-y: hidden;
}


has-ie-6 #grid {
	width: 100%;
	/* Give the grid a height.  Not optimal, but we'd need more JS for better. */
	height: 20em;
}

#grid .dgrid-header-scroll {
	display: none;
}
#grid .dgrid-header {
	right: 0;
}

</style>


<script type="text/javascript">
	
function unique(arr){
	//	create a unique list of items from the passed array
	//	(removing duplicates).  This is quick and dirty.

	//	first, set up a hashtable for unique objects.
	var obj = {};
	for(var i=0,l=arr.length; i<l; i++){
		if(!(arr[i] in obj)){
			obj[arr[i]] = true;
		}
	}

	//	now push the unique objects back into an array, and return it.
	var ret = [];
	for(var p in obj){
		ret.push(p);
	}
	ret.sort();
	return ret;
}	
	
	
	
require(
["dijit/Dialog", "dojo/_base/xhr", "dojo/_base/declare",
"dojo/store/Memory", "dojo/store/Observable", 
"dgrid/Grid", "dgrid/extensions/Pagination",
"dgrid/Selection", "dgrid/Keyboard", "dgrid/GridFromHtml",
"dojo/_base/lang", "dojo/_base/declare", "dojo/dom-construct", 
"dojo/dom-form", "dgrid/test/data/base", "dojo/domReady!"],
function(Dialog, xhr, declare, 
	Memory, Observable, 
	Grid, Pagination, 
	Selection, Keyboard, GridFromHtml,
	lang, declare, domConstruct, domForm){

	var dialog = new Dialog({
        });
	
	xhr.get({
			// URL de la request
			url: '{{=URL(r = request, c = "correo", f="correo_load_mails")}}',
			handleAs: "json",
			content: {
			},
			load: function(response) {
				cargar_grilla(response.data_list, response.rows);
			}
	});
	
	cargar_grilla = function(data_list, rows){
		

		
	data = {
		identifier: 'id',
		label: 'id',
		items: []
	};
	
	
	for(var i=0, l=data_list.length; i<rows; i++){
		data.items.push(lang.mixin({ id: i }, data_list[i%l]));
	}
	
	// global var testStore
	testStore = Observable(new Memory({data: data}));
	
	var CustomGrid = declare([Grid, Keyboard, Selection, Pagination]);
	
	getColumns2 = function(){
		return {
		Leido: {
				label: "Leido",
				sortable: true,
				formatter: function(icon){
					if (icon){ 
						leido = true;
						return '<div class="icon" style="background-repeat: no-repeat; background-image:url(../static/images/mail/emailopenwhite32.png);">&nbsp;</div>';
					}
					else{
						leido = false;
						return '<div class="icon" style="background-repeat: no-repeat; background-image:url(../static/images/mail/emailcloseorange32.png);">&nbsp;</div>';
					}
				},
			},
		From: 'From',
		Subject: 'Subject',
		Fecha: 'Fecha'
		};
	};	
	
	window.grid = new CustomGrid({
		store: testStore,
		columns: getColumns2(),
		pagingLinks: false,
		pagingTextBox: false,
		firstLastArrows: true,
		pageSizeOptions: [10, 30, 50]
		}, "grid");
		

	document.getElementById("resolverSelected").onclick = function(){
		
		for (var i in grid.selection) {
			
			xhr.get({
			// URL de la request
			url: "{{=URL(r = request, c = 'correo', f = 'correo_is_leido')}}",
			handleAs: "json",
			content: {
				id_mail : i
			},
			load: function(response) {
				if (response.leido == false){
					window.location = "{{=URL(r = request, c = 'correo', f = 'correo_resolve')}}/" + i;
				}
				else{
					var result = '<strong>Atenci&oacuten!</strong></br>';
				    result += 'El mail que intenta resolver ya fue abierto</br>';
				    result += 'Puede eliminarlo de la bandeja.';
					dialog.set("content", result);
					dialog.show();
				}
			}})
		}
	};
	
	
	showDialog_datos_mail = function(response){
		var result = '<p><strong>Contenido</strong></p>';
		result += '<div style="height:295px;overflow-y:scroll;border:1px solid #769dc4;padding:0 10px;width:310px">';
	    result += '<label>From: </label>' + " " + response.From;
	    result += '</br>';
	    result += '<label>Subject: </label>' + " " + response.Subject;
	    result += '</br>';
	    result += '<label>Message: </label>' + " " + response.Text;
	       
	    result +=  '</div>';

	    dialog.set("content", result);
		dialog.show();
	}
		
	document.getElementById("verSelected").onclick = function(){
		
		for (var i in grid.selection) {
			
			xhr.get({
					// URL de la request
					url: '{{=URL(r = request, c = "correo", f="correo_load_mail_entrada")}}',
					handleAs: "json",
					// Enviar los datos para checkear en la db 
					content: {
						id_mail : i
					},
					load: function(response) {
						showDialog_datos_mail(response);

					}
			});
		}
	};
	
	document.getElementById("deleteSelected").onclick = function(){
		for (var i in grid.selection) {
			var result = '<strong>Atenci&oacute;n!</strong></br>';
			result +=  'Confirma la eliminaci&oacute;n del mail seleccionado?</br></br>';
			result += '<button onclick="confirm_delete();">Confirmar</button>';
		    dialog.set("content", result);
			dialog.show();
		}
	};
	
	confirm_delete = function(){
		dialog.hide();
		for (var i in grid.selection) {
			xhr.get({
				url: '{{=URL(r = request, c = "correo", f="correo_delete")}}',
				handleAs: "json",
				content: {
					id_mail : i
				},
				load: function(response) {
					window.location = "{{=URL(r = request, c = 'correo', f = 'correo_entrada')}}";

					}
			});
		}
	}
	
	};
	
})
</script>

<button type="button" id="verSelected">Ver</button>
<button type="button" id="resolverSelected">Resolver</button>
<button type="button" id="deleteSelected">Eliminar</button>

{{=form}}